package personal.hmm.hpbd;

import personal.hmm.google.authenticator.GoogleAuthenticator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AuthenScreen extends Activity {
	private final String SECRET_CODE = "N33AVE6LK75MTBUV";
	private EditText edAuthen;
	private Button btConfirm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.authen);
		Toast.makeText(getApplicationContext(), R.string.volume,
				Toast.LENGTH_LONG).show();
		// All widgets
		btConfirm = (Button) findViewById(R.id.btConfirm);
		btConfirm.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				if (checkAuthen()) {
					startActivity(new Intent("personal.hmm.hpbd.MAIN"));
				} else {
					Toast.makeText(getApplicationContext(),
							"Your code is not authenticated.", Toast.LENGTH_SHORT)
							.show();
				}
			}
		});
	}

	private String typeCode() {
		edAuthen = (EditText) findViewById(R.id.edAuthen);
		String typeCode = edAuthen.getText().toString();
		return typeCode;
	}

	private boolean checkAuthen() {
		long timeMsec = System.currentTimeMillis();
		GoogleAuthenticator authenCode = new GoogleAuthenticator(SECRET_CODE,
				timeMsec);
		if (authenCode.checkCode(typeCode())) {
			edAuthen.setText(null);
			return true;
		} else {
			edAuthen.setText(null);
			return false;
		}
	}
}
